package someTestExcemple.huaweiOd.unionFindSet;

//华为OD机试 - 工单调度策略 - 并查集（Java 2024 E卷 200分）
//https://blog.csdn.net/guorui_java/article/details/142664725
import java.util.*;
//思路：贪心+并查集
public class JobOrderSchedulingStrategy {
    private static int[] parent;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //工单数量
        int N = sc.nextInt();
        Job[] jobs = new Job[N];
        int maxDeadLine =0;
        for(int i=0;i<N;i++) {
            int deadLine = sc.nextInt();
            maxDeadLine = Math.max(maxDeadLine,deadLine);
            int score =sc.nextInt();
            jobs[i] = new Job(deadLine,score);
        }
        sc.close();
        //按积分降序，同积分，按规定截至时间升序
        Arrays.sort(jobs);
        System.out.println(Arrays.toString(jobs));
        //按最大截至时间初始化并查集
        int nowTime=0;
        int totalScore = 0;
        for(Job job:jobs) {
            if(nowTime<job.deadLine) {
                totalScore+= job.score;
                nowTime++;
            }
        }
        System.out.println(totalScore);

    }

    public static class Job implements Comparable<Job> {
        int deadLine; //截止时间
        int score;//奖励积分
        public Job(int deadLine,int score) {
            this.deadLine =deadLine;
            this.score =score;
        }


        @Override
        public int compareTo(Job other) {
            if (this.score != other.score) {
                return other.score - this.score; // 降序排列积分
            } else {
                return this.deadLine - other.deadLine; // 升序排列SLA时间
            }
        }

        @Override
        public String toString() {
            return "Job{" +
                    "deadLine=" + deadLine +
                    ", score=" + score +
                    '}';
        }
    }

    //初始化并查集合

    public static void initUnionSet(int maxDeadLine) {

    }
}

/**
 * 5
 * 2 10
 * 1 5
 * 2 7
 * 1 8
 * 3 6
 */